package com.thoughtworks.conference.dao.impl;

import com.thoughtworks.conference.constants.CommonConstants;
import com.thoughtworks.conference.constants.SystemMessageConstants;
import com.thoughtworks.conference.dao.ConferenceTalkDao;
import com.thoughtworks.conference.exception.ConferenceTrackException;
import com.thoughtworks.conference.model.TalkInfo;
import com.thoughtworks.conference.model.configmodel.ErrorMessage;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: 会议信息的数据接口实现类 </p>
 * <p>Description:用于获取会议信息</p>
 *
 * @author <a href="mailTo:bingxuewulei@outlook.com">wanglei</a>
 * @version 1.0
 * @history: Created by wanglei on  2019/4/23
 */
public class ConferenceTalkDaoImpl implements ConferenceTalkDao {
    @Override
    public List<TalkInfo> feachTalkInfo(String dataFilePath) {
        //文件输入流
        FileInputStream inputStream ;
        //获取到数据结合
        List<TalkInfo> talkInfoList = new ArrayList<>();
        if(StringUtils.isEmpty(dataFilePath)){
            throw new ConferenceTrackException(SystemMessageConstants.MESSAGE_DATA_FILE_PATH_EMPTY);
        }
        try{
            inputStream = new FileInputStream(dataFilePath);
        }catch (FileNotFoundException e){
            throw new ConferenceTrackException("Path ["+dataFilePath+"] data file could not be found ,Please confirm whether the path is correct");
        }
        // 缓存读取对象
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        // 行号
        int lineNum = 1;
        try {
            String dataStrLine ;
            while ((dataStrLine = bufferedReader.readLine())!=null){
                // 如果是注释或者为空则不读取该条记录
                if(StringUtils.isEmpty(dataStrLine)||dataStrLine.contains(CommonConstants.COMENT_FLG)){
                  continue;
                }
                // 获取会议的title信息
                int titleSplitIndex = dataStrLine.lastIndexOf(" ");
                if(titleSplitIndex<0){
                    throw new ConferenceTrackException((new ErrorMessage(SystemMessageConstants.MESSAGE_DATA_FORMAT_ERROR, lineNum).toString()));
                }
                // 会议标题
                String talkTitle = dataStrLine.substring(0, titleSplitIndex);
                //会议时间和单位
                String talkTimAndTimeUnitStr =  dataStrLine.substring(titleSplitIndex+1);
                String talkTimeStr = "";
                if(StringUtils.isEmpty(talkTitle)){
                    throw new ConferenceTrackException(new ErrorMessage(SystemMessageConstants.MESSAGE_TITLE_EMPTY,lineNum).toString());
                }
                if(StringUtils.isEmpty(talkTimAndTimeUnitStr)){
                    throw new ConferenceTrackException(new ErrorMessage(SystemMessageConstants.MESSAGE_TIME_AND_UINT_EMPTY,lineNum).toString());
                }
                //去掉时间中的数字获得时间单位
                String talkTimeUnitStr = talkTimAndTimeUnitStr.replaceAll("\\d+", "");
                if(StringUtils.isEmpty(talkTimeUnitStr)){
                    throw new ConferenceTrackException(new ErrorMessage(SystemMessageConstants.MESSAGE_TIME_UINT_EMPTY,lineNum).toString());
                }
                //去掉时间中非数字字符获取会议时长
                String talkMinutes = talkTimAndTimeUnitStr.replaceAll("\\D+", "");
                //如果会议单位不是lightning且会议时长为空则为非法数据
                if(StringUtils.isEmpty(talkMinutes)
                        &&!CommonConstants.LIGHTNING_TALK.equals(talkTimeUnitStr)){
                    throw new ConferenceTrackException(new ErrorMessage(SystemMessageConstants.MESSAGE_TIME_EMPTY,lineNum).toString());
                }
                //处理分钟类型的数据
                int intTalkMinutes = 0;
                try {
                    if(StringUtils.isNotEmpty(talkMinutes)){
                        intTalkMinutes = Integer.parseInt(talkMinutes);
                        talkTimeStr = talkMinutes;
                    }
                } catch (NumberFormatException e) {
                    throw new ConferenceTrackException(new ErrorMessage(SystemMessageConstants.MESSAGE_FORMAT_TIME_ERROR,lineNum).toString());
                }
                //处理lightning单位类型的数据
                if (CommonConstants.LIGHTNING_TALK.equals(talkTimeUnitStr)) {
                    if(StringUtils.isNotEmpty(talkMinutes)){
                        intTalkMinutes = CommonConstants.LIGHTNING_TALK_VALUE * intTalkMinutes;
                        talkTimeStr = talkMinutes;
                    }else{
                        intTalkMinutes = CommonConstants.LIGHTNING_TALK_VALUE;
                        talkTimeStr = "";
                    }
                }
                TalkInfo talkInfo = new TalkInfo(talkTitle,talkTimeStr,intTalkMinutes ,talkTimeUnitStr);
                talkInfoList.add(talkInfo);
                lineNum ++;
            }
        } catch (IOException e) {
            throw new ConferenceTrackException((e.getMessage()));
        }finally {
            try {
                inputStream.close();
                bufferedReader.close();
            } catch (IOException e){
                throw new ConferenceTrackException((e.getMessage()));
            }
        }
        //如果数据文件文件为空给出对应的提示
        if(talkInfoList.isEmpty()){
            throw new ConferenceTrackException(SystemMessageConstants.MESSAGE_DATA_FILE_EMPTY);
        }
        return talkInfoList;
    }

}
